const {defineConfig} = require('@vue/cli-service')

const os = require('os');

function getNetworkIp() {
	// 打开的 host
	let needHost = '';
	try {
		// 获得网络接口列表
		let network = os.networkInterfaces();
		for (let dev in network) {
			let iface = network[dev];
			for (let i = 0; i < iface.length; i++) {
				let alias = iface[i];
				if (
					alias.family === 'IPv4' &&
					alias.address !== '127.0.0.1' &&
					!alias.internal
				) {
					needHost = alias.address;
				}
			}
		}
	} catch (e) {
		needHost = 'http://localhost';
	}
	return needHost;
}

module.exports = defineConfig({
	//设置为空打包后不分更目录还是多级目录
	publicPath: '',
	//build编译后存放静态文件的目录
	//assetsDir: "static",

	// build编译后不生成资源MAP文件
	productionSourceMap: false,

	//开发服务,build后的生产模式还需nginx代理
	devServer: {
		allowedHosts: 'all',
		open: false, //运行后自动打开浏览器
		port: process.env.VUE_APP_PORT, //挂载端口
		proxy: {
			'/scui': {
				target: process.env.VUE_APP_API_BASEURL,
				ws: true,
				pathRewrite: {
					'^/scui': '/'
				}
			},
			'/jkdy': {
				target: process.env.VUE_APP_JKDY_API_BASEURL,
				ws: true,
				pathRewrite: {
					'^/jkdy': '/'
				}
			},
			'/superApi': {
				//http://localhost:2800 /api /authorization/loginByAccountPassword
				//http://101.89.156.213:9000/api  /authorization/loginByAccountPassword
				target: process.env.VUE_APP_SYS_API_BASEURL,
				ws: true,
				pathRewrite: {
					'^/superApi': ''
				}
			}
		}
	},

	chainWebpack: config => {
		// 获得网络接口列表
		let network = getNetworkIp();
		console.log('network:' + network);
		process.env.VUE_APP_NETWORK_INFO = network;
		process.env.VUE_APP_OS = network;
		console.log('process.env.VUE_NETWORK_INFO' + process.env.VUE_APP_NETWORK_INFO)
		console.log('process.env.VUE_OS' + JSON.stringify(process.env.VUE_APP_OS))

		// 移除 prefetch 插件
		config.plugins.delete('preload');
		config.plugins.delete('prefetch');
		config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js');
	},

	configureWebpack: {
		//性能提示
		performance: {
			hints: false
		},
		optimization: {
			splitChunks: {
				chunks: "all",
				automaticNameDelimiter: '~',
				name: "scuiChunks",
				cacheGroups: {
					//第三方库抽离
					vendor: {
						name: "modules",
						test: /[\\/]node_modules[\\/]/,
						priority: -10
					},
					elicons: {
						name: "elicons",
						test: /[\\/]node_modules[\\/]@element-plus[\\/]icons-vue[\\/]/
					},
					tinymce: {
						name: "tinymce",
						test: /[\\/]node_modules[\\/]tinymce[\\/]/
					},
					echarts: {
						name: "echarts",
						test: /[\\/]node_modules[\\/]echarts[\\/]/
					},
					xgplayer: {
						name: "xgplayer",
						test: /[\\/]node_modules[\\/]xgplayer.*[\\/]/
					},
					codemirror: {
						name: "codemirror",
						test: /[\\/]node_modules[\\/]codemirror[\\/]/
					}
				}
			}
		}
	}
})
